<?php
/**
 * 平台账号
 * (model)
 */

namespace App\Models\Basics;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class SysPlatAccount extends Model {

    protected $connection = 'mysql';

    protected $table = 'b_plat_account';

    protected $guarded = [];

    //关联平台
    public function sys_plat(){
        return $this->belongsTo(SysPlat::class, 'plat_id');
    }
    //亚纳逊平台账号
    public function amazon_account(){
        return $this->hasOne(AmazonAccount::class, 'account_id');
    }
    //独立站平台账号
    public function shopify_account(){
        return $this->hasOne(ShopifyAccount::class, 'account_id');
    }
    //乐天平台账号
    public function rakuten_account(){
        return $this->hasOne(RakutenAccount::class, 'account_id');
    }

    /**
     * 账号选项（全）
     * @return \Illuminate\Support\Collection
     */
    public static function optionsAll(){
        return self::query()->pluck('account', 'id');
    }

    /**
     * 平台账号选项
     * @param null $plat_id
     * @return \Illuminate\Support\Collection
     */
    public static function options($plat_id = null){
        $query = self::query();
        if ($plat_id) {
            $query->where('plat_id', $plat_id);
        }
        return $query->pluck('account', 'id');
    }

    //账号关联的所有国家
    public static function getCountriesById($id){
        return DB::table('b_amazon_account_country')->where('account_id',$id)->pluck('country_id');
    }

    /**
     * 依据账号id获取账号名称
     * @param $id
     * @return mixed|string
     */
    public static function getAccountById($id){
        return self::query()->where('id', $id)->value('account');
    }

    /**
     * 依据平台与账号获取账号id
     * @param $plat_id
     * @param $account
     * @return mixed
     */
    public static function getIdByAccount($account, $plat_id = 0){

        $query = self::query();
        $query->where('account', $account);
        if ($plat_id > 0) {
            $query->where('plat_id', '=', $plat_id);
        }
        $accountId = $query->value('id');

        return $accountId ?? 0;
    }

    /**
     * 更新平台账号
     * @param $id
     * @param $platId
     * @param $account
     */
    public static function updatePlatAccount($id, $platId, $account){
        $model = self::query()->where('id', $id)->first();
        if (!$model) {
            self::createPlatAccount($id,$platId,$account);
        }else{
            $model->account = $account;
            $model->save();
        }
    }

    /**
     * 创建平台账号
     * @param $id
     * @param $platId
     * @param $account
     */
    public static function createPlatAccount($id, $platId, $account){
        self::query()->create([
            'id' => $id,
            'plat_id' => $platId,
            'account' => $account,
        ]);
    }

    /**
     * 获取平台账号组 (Amazon平台账号 + Rakuten平台账号)
     * [
     *   33 => "W-账号1-JP"
     *   47 => "账号100-EU"
     *   ...
     *   100000 => "shopify-s1"
     *   100001 => "shopify-s2"
     *   ...
     *   200000 => "rakuten-r1"
    200001 => "rakuten-r2"
     * ]
     * @return array
     */
    public static function getPlatAccountArray(){
        $amazonAccount  = AmazonAccount::options()->toArray(); //amazon账号
        $shopifyAccount = ShopifyAccount::options()->toArray(); //shopify账号
        $rakutenAccount = RakutenAccount::options()->toArray(); //rakuten账号

        return $amazonAccount + $shopifyAccount + $rakutenAccount;
    }

    /**
     * 依据平台账号ID, 获取平台账号名称
     * [
     *   "W-账号1-JP" => 33
     *   "账号100-EU" => 47
     *   ...
     *   "shopify-s1" => 100000
     *   "shopify-s2" => 100001
     *   ...
     *   "rakuten-r1" => 200000
     *   "rakuten-r2" => 200001
     * ]
     * @param $accountId
     * @return array
     */
    public static function getPlatAccount($accountId){
        $amazonAccount  = AmazonAccount::options()->toArray();
        $shopifyAccount = ShopifyAccount::options()->toArray();
        $rakutenAccount = RakutenAccount::options()->toArray();
        $platAccount = $amazonAccount + $shopifyAccount + $rakutenAccount;

        return $platAccount[$accountId];
    }

}
